package com.example.myapplication;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private Map<String, Action> mStringActionMap = new HashMap<>();
    private EditText mEditText;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button btn = findViewById(R.id.btn_test);
        int a = 2;
        mEditText = findViewById(R.id.edit_query);
        int test1 = 2;
        int test2 = 3;
        int test3 = 4;
        int test4 = 5;
        int test5 = 3;
        int test6 = 6;
        int test7 = 7;
        String needStash = "37";
        int test8 = 8;
        int test9 = 9;
        int test10 = 10;
        int test11 = 11;
        int test12 = 12;
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // btn.performClick();
                query();
            }
        });
        List<String> list = new ArrayList<>();
        list.add(getResources().getString(R.string.test1));
        list.add(getResources().getString(R.string.test1));
        list.add(getResources().getString(R.string.test1));
        list.add(getResources().getString(R.string.test1));
        list.add(getResources().getString(R.string.test1));
        for (String s : list) {
            Log.e("List", System.identityHashCode(s) + "");
        }
//        String str = getResources().getString(R.string.test1);
//        mStringActionMap.put(str, this::funcation1);
//        str = getResources().getString(R.string.test2);
//        mStringActionMap.put(str, this::funcation2);
//       str = getResources().getString(R.string.test1);
//        mStringActionMap.put(str, this::funcation2);


//        ActivityCompat.requestPermissions(this,
//                new String[]{"com.patac.hmi.vehiclehealth.global.search.read"}, 1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // sendNotification(MainActivity.this, "title", "测试通知");
                // NaviautoAPIManager.getInstance().jumpToSearchPage("加油站");
                //NaviautoAPIManager.getInstance().openMap();
                // NaviautoAPIManager.getInstance().jumpToSearchPage("加油站");
//                Location location = NaviautoAPIManager.getInstance().getCurrentLocation();
                Log.e(TAG, "点击");
            }
        });
//        NaviautoAPIManager.getInstance().init(getApplicationContext(), "com.example.myapplication",
//                "");

        Log.e(TAG, "test commit");
    }

    private void funcation1() {
        Log.e(TAG, "funcation1");
    }

    private void funcation2() {
        Log.e(TAG, "funcation2");
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void sendNotification(Context context, String title, String text) {
        Bundle bundle = new Bundle();
        bundle.putInt("patac_notification_type", 0);
        //通知类消息
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.patac.hmi.vehiclehealth", "com.patac.hmi.vehiclehealth.presentation.ui.VehicleHealthActivity");
        intent.setComponent(componentName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        String channelId = createNotificationChannel(context, "Vehicle_Info_Id", "VehicleInfo_Name", NotificationManager.IMPORTANCE_HIGH);

        NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channelId)
                .setContentTitle(title)
                .setContentText(text)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .addExtras(bundle)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .addAction(new NotificationCompat.Action(0, "跳转到最近加油站",
                        pendingIntent))
                .setAutoCancel(true);
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(100, notification.build());
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private String createNotificationChannel(Context context, String channelID, String channelNAME, int level) {
        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(channelID, channelNAME, level);
        }
        manager.createNotificationChannel(channel);
        return channelID;
    }

    private void query() {
        String uri_str = "content://com.patac.hmi.vehiclehealth.provider/GlobalSearch";
        //中文搜索
        String search_str = mEditText.getText().toString();//注意：搜索的字符串不能为空。
        //英文搜索
        // String search_str = "Engine";
        Uri uri = Uri.parse(uri_str + "/" + search_str);
        Cursor c = getContentResolver().query(uri, null, null, null, null);
        if (c.getCount() > 0) {
            while (c.moveToNext()) {
                //获取id
                int id = c.getInt(c.getColumnIndex("key"));
                //获取中文字符
                String nameCn = c.getString(c.getColumnIndex("name_cn"));
                //获取英文字符
                String nameEn = c.getString(c.getColumnIndex("name_en"));
                Log.e(TAG, "result=" + nameCn);
            }
        }
        if (c != null) {
            c.close();
        }
    }

    private void jump(int key) {
        Intent intent = new Intent("com.patac.hmi.vehiclehealth.VIEW");
        intent.putExtra("ENTRY_TYPE_EXTRA", 1);//进入方式，值设置为固定值1。
        intent.putExtra("BACK_TYPE_EXTRA", 0);//退出方式，值设置为固定值0。
        intent.putExtra("KEY_EXTRA", key);//跳转XX界面.该处的id就是上面搜索中得到的id。
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public interface Action {
        /**
         * Runs the action and optionally throws a checked exception.
         *
         * @throws Exception if the implementation wishes to throw a checked exception
         */
        void run() throws Exception;
    }

}

